package com.blog.controller;

import com.blog.common.Constants;
import com.blog.entity.SysJob;
import com.blog.entity.SysUser;
import com.blog.service.SysJobService;
import com.blog.util.job.ScheduleUtils;
import com.blog.util.job.CronUtils;

import com.blog.util.dto.AjaxResult;
import com.blog.util.laydto.LayDto;
import com.blog.util.laydto.LayDtoUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.quartz.SchedulerException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author cgx
 * @Date 2022/1/6 9:33
 */
@Controller
@Slf4j
public class SysJobController {

    @Resource
    SysJobService jobService;
    
    /**
     * 定时任务基础跳转类
     * @param
     * @return
     */
    @GetMapping("admin/system/task/task-index")
    public String index(){
        return "admin/task/task-index";
    }

    @GetMapping("/admin/task/add")
    public String addJob(){
        return "admin/task/task-add";
    }

    /**
     * 后台任务管理表格数据
     * @param
     * @return
     */
    @GetMapping("admin/task/table")
    @ResponseBody
    public LayDto taskTable(){
        List<SysJob> list = jobService.list();
        return LayDtoUtil.returnSuccess(0,"",list.size(),list);
    }

    @PostMapping("admin/task/changeJobState")
    @ResponseBody
    public AjaxResult changeJobState(@RequestParam("jobId") String jobId,@RequestParam("sign") String sign) throws SchedulerException {
        SysJob sysJob = jobService.getById(jobId);
        if (sign.equals("ON")){
            sysJob.setStatus("0");
        }else {
            sysJob.setStatus("1");
        }
        jobService.changeStatus(sysJob);
        return AjaxResult.success();
    }

    @PostMapping("admin/job/saveJob")
    @ResponseBody
    public AjaxResult saveJob(SysJob job) throws Exception {
        log.info("新增任务如下: {}",job.toString());
        if (!CronUtils.isValid(job.getCronExpression())) {
            return AjaxResult.error("新增任务'" + job.getJobName() + "'失败，Cron表达式不正确");
        }
        else if (StringUtils.containsIgnoreCase(job.getInvokeTarget(), Constants.LOOKUP_RMI))
        {
            return AjaxResult.error("新增任务'" + job.getJobName() + "'失败，目标字符串不允许'rmi'调用");
        }
        else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), new String[] { Constants.LOOKUP_LDAP, Constants.LOOKUP_LDAPS }))
        {
            return AjaxResult.error("新增任务'" + job.getJobName() + "'失败，目标字符串不允许'ldap'调用");
        }
        else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), new String[] { Constants.HTTP, Constants.HTTPS }))
        {
            return AjaxResult.error("新增任务'" + job.getJobName() + "'失败，目标字符串不允许'http(s)'调用");
        }
        else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), Constants.JOB_ERROR_STR))
        {
            return AjaxResult.error("新增任务'" + job.getJobName() + "'失败，目标字符串存在违规");
        }
        else if (!ScheduleUtils.whiteList(job.getInvokeTarget()))
        {
            return AjaxResult.error("新增任务'" + job.getJobName() + "'失败，目标字符串不在白名单内");
        }
        Subject subject = SecurityUtils.getSubject();
        SysUser sysUser = (SysUser)subject.getPrincipal();

        job.setCreatePeople(sysUser.getUsername());
        job.setActive(1);
        job.setStatus("0");

        jobService.saveJob(job);
        return AjaxResult.success();
    }


}
